package com.pubsys.main.client.componentes.campodados;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.uibinder.client.UiHandler;

public class IntTextBox extends Composite {

	private static IntTextBoxUiBinder uiBinder = GWT
			.create(IntTextBoxUiBinder.class);
	@UiField TextBox textBox;
	
	private int min ;
	private int max ;
	

	interface IntTextBoxUiBinder extends UiBinder<Widget, IntTextBox> {
	}

	public IntTextBox() {
		initWidget(uiBinder.createAndBindUi(this));
		textBox.setText("");
		min = 1;
		max = 31;
		ini();
	}
	
	public String getText(){
		return textBox.getText();
		
	}
	
	public int getInt(){
		if(this.textBox.getText().trim().equals(""))
			return 0;
		
		return Integer.parseInt(this.textBox.getText());
	}
	
	public void setFocus(boolean focus){
		this.textBox.setFocus(focus);
	}
	
	@SuppressWarnings("deprecation")
	private void ini(){
		this.textBox.addKeyboardListener(new KeyboardListenerAdapter() {
	      public void onKeyPress(Widget sender, char keyCode, int modifiers) {
	        if ((!Character.isDigit(keyCode)) && (keyCode != (char) KEY_TAB)
	            && (keyCode != (char) KEY_BACKSPACE)
	            && (keyCode != (char) KEY_DELETE) && (keyCode != (char) KEY_ENTER) 
	            && (keyCode != (char) KEY_HOME) && (keyCode != (char) KEY_END)
	            && (keyCode != (char) KEY_LEFT) && (keyCode != (char) KEY_UP)
	            && (keyCode != (char) KEY_RIGHT) && (keyCode != (char) KEY_DOWN)) {
	          // TextBox.cancelKey() suppresses the current keyboard event.
	          ((TextBox)sender).cancelKey();
	        }
	        else {
//	        	String str = textBox.getText();
//	        	str.concat( String.valueOf(keyCode) );
//	        	int nro = Integer.parseInt(str);
//	        	if((nro < min ) || (nro > max))
//	        		((TextBox)sender).cancelKey();
//	        	//str.concat()
//	        	System.out.println(textBox.getText() + " - " + nro);
	        }
	      }
	    });
	}
	
	
	
	public void setValue(Integer value){
		if (value!= null)
			this.textBox.setText(value.toString());
		
	}

}
